//     THIS CODE USES CODE FROM AI SIGNALS GOLD!!! 
//why ?    because i was bored and i was lazy so i did not want to put all my freetime on this but i did add extra options
//     disclaimer : this does nt contain all options... if there is something you would like me to add dm me 
// @hawkeye#6019 <----------- discord id
//     
//-----------------------------------------====================================================-------------------------------\\
//                                                                                                                             \\
//@version=5
indicator("EzAlgo V.5", shorttitle='EzTrades V.5', overlay=true)

// -------------------------- user inputs ----------------------- \\
enSignals = input.bool(true, 'Enable Signals', group='Buy & Sell')
sens      = input.float(2, 'Sensitivity')
enrev = input.bool(true, 'Diamond Signals', group='Signals', tooltip='IDK what this s called so Ijust called it diamond signals')

ensr = input.bool(true, 'Enable Support And Resistance', group='S & R')
ensra = input.bool(true, 'Enable Support And Resistance Zones', group='S & R')

enCloud   = input.bool(true, 'Enable Trend Ribbon', group='Cloud')
volBands = input.bool(true, 'Show Bands', group='Cloud')
mal       = input.bool(false, 'Show Trend Moving Average Line', inline='s')
mals      = input.int(80, '', inline='s')

levelsl      = input.bool(false, "Sl 1", group="TP & SL")
levels1      = input.bool(false, "TP 1", group="TP & SL", inline='tp1')
r1           = input.float(0.7, '', group='TP & SL', inline='tp1')
levels2      = input.bool(false, "TP 2", group="TP & SL", inline='tp2')
r2           = input.float(1.2, '', group='TP & SL', inline='tp2')
levels3      = input.bool(false, "TP 3", group="TP & SL", inline='tp3')
r3           = input.float(1.5, '', group='TP & SL', inline='tp3')
atrRisk     = input.int(defval=4, minval=1, maxval=4, title="Risk To Reward", group="TP & SL")

// -------------------------- signal function ------------------------ \\ based on free algo's ai signal gold
supertrend(_src, factor, atrLen) =>
	atr = ta.atr(atrLen)
	upperBand = _src + factor * atr
	lowerBand = _src - factor * atr
	prevLowerBand = nz(lowerBand[1])
	prevUpperBand = nz(upperBand[1])
	lowerBand := lowerBand > prevLowerBand or close[1] < prevLowerBand ? lowerBand : prevLowerBand
	upperBand := upperBand < prevUpperBand or close[1] > prevUpperBand ? upperBand : prevUpperBand
	int direction = na
	float superTrend = na
	prevSuperTrend = superTrend[1]
	if na(atr[1])
		direction := 1
	else if prevSuperTrend == prevUpperBand
		direction := close > upperBand ? -1 : 1
	else
		direction := close < lowerBand ? 1 : -1
	superTrend := direction == -1 ? lowerBand : upperBand
	[superTrend, direction]
[supertrend, direction] = supertrend(close, sens, 11)
sma9 = ta.sma(close, 13)

bull = ta.crossover(close, supertrend)
bear = ta.crossunder(close, supertrend)

y1 = low - (ta.atr(30) * 2)
y2 = high + (ta.atr(30) * 2)

// ---------------------------------- plot buy & sell signal -------------------------- \\ based on free ai signal gold

buy  = enSignals and bull ? label.new(bar_index, y1, "Buy", xloc.bar_index, yloc.price, #00DBFF, label.style_label_up, color.white, size.normal) : na
sell = enSignals and bear ? label.new(bar_index, y2, "Sell", xloc.bar_index, yloc.price, #E93E63, label.style_label_down, color.white, size.normal) : na

// ---------------------------- cloud ---------------------------- \\
candle  = ta.sma(close, 7)
reach   = ta.sma(close, 13)
candlep = plot(enCloud ? candle : na, color=color.new(color.white, 100))
reachp  = plot(enCloud ? reach  : na, color=color.new(color.white, 100))
fill(reachp, candlep, color= candle > reach ? color.new(#00DBFF, 75) : color.new(#E93E63, 75))


barcolor(close > supertrend ? #00DBFF99 : #E93E6399)


// ====================--------------------- tp & sl -----------------=================== \\ based on free algo's ai signal gold


lvlLines    = (true)
linesStyle  = ("SOLID")
lvlDecimals = (4)
lvlDistance = (1)
atrLen      = (14)

decimals  = lvlDecimals == 1 ? "#.#" : lvlDecimals == 2 ? "#.##" : lvlDecimals == 3 ? "#.###" : lvlDecimals == 4 ? "#.####" : lvlDecimals == 5 ? "#.#####" : lvlDecimals == 6 ? "#.######" : lvlDecimals == 7 ? "#.#######" : "#.########"
trigger = bull ? 1 : 0
trigger2 = bear ? 0 : 1
atrBand = ta.atr(atrLen) * atrRisk
atrStop = trigger == 1 ? low - atrBand : high + atrBand
atrStop2 = trigger2 == -1 ? high + atrBand : low - atrBand

countBull = ta.barssince(bull)
countBear = ta.barssince(bear)

lastTrade(src) => ta.valuewhen((bull) or (bear), src, 0)
entry = levelsl or levels1 or levels2 or levels3 ? label.new(time, close, "Entry | " + str.tostring(lastTrade(close), decimals), xloc.bar_time, yloc.price, color.new(color.gray ,0), label.style_label_left, color.white, size.small) : na
label.set_x(entry, label.get_x(entry) + math.round(ta.change(time) * lvlDistance))
label.set_y(entry, lastTrade(close))
label.delete(entry[1])

stop_y = lastTrade(atrStop)
stop  = levelsl ? label.new(time, close, "Stop Loss | " + str.tostring(stop_y, decimals), xloc.bar_time, yloc.price, color.rgb(255,82,83,0), label.style_label_left, color.new(color.white, 10), size.small) : na
label.set_x(stop, label.get_x(stop) + math.round(ta.change(time) * lvlDistance))
label.set_y(stop, stop_y)
label.delete(stop[1])

tp1_y = (lastTrade(close)-lastTrade(atrStop))*r1 + lastTrade(close)
tp1   = levels1 ? label.new(time, close, "Take Profit 1 | " + str.tostring(tp1_y, decimals), xloc.bar_time, yloc.price, color.rgb(0,217,252), label.style_label_left, color.black, size.small) : na
label.set_x(tp1, label.get_x(tp1) + math.round(ta.change(time) * lvlDistance))
label.set_y(tp1, tp1_y)
label.delete(tp1[1])

tp1s = ta.crossover(close, tp1_y) and not bull and close>supertrend

plotshape(tp1s, text='', style=shape.xcross, location=location.abovebar, size=size.tiny, color=color.new(#ff000099, 68), textcolor=#2157f999)

tp1sd = ta.crossunder(close, tp1_y) and not bull and not bear and close<supertrend

plotshape(tp1sd, text='', style=shape.xcross, location=location.belowbar, size=size.tiny, color=color.new(#2157f999, 68), textcolor=#ff000099)

tp15_y = (lastTrade(close)-lastTrade(atrStop))*r2 + lastTrade(close)
tp15   = levels2 ? label.new(time, close, "Take Profit 2 | " + str.tostring(tp15_y, decimals), xloc.bar_time, yloc.price, color.rgb(0,217,252), label.style_label_left, color.black, size.small) : na
label.set_x(tp15, label.get_x(tp15) + math.round(ta.change(time) * lvlDistance))
label.set_y(tp15, tp15_y)
label.delete(tp15[1])

tp2s = ta.crossover(close, tp15_y) and not bull and close>supertrend

plotshape(tp2s, text='', style=shape.xcross, location=location.abovebar, size=size.tiny, color=color.new(#ff000099, 68), textcolor=#2157f999)

tp2sd = ta.crossunder(close, tp15_y) and not bull and not bear and close<supertrend

plotshape(tp2sd, text='', style=shape.xcross, location=location.belowbar, size=size.tiny, color=color.new(#2157f999, 68), textcolor=#ff000099)

tp2_y = (lastTrade(close)-lastTrade(atrStop))*r3 + lastTrade(close)
tp2   = levels3 ? label.new(time, close, "Take Profit 3 | " + str.tostring(tp2_y, decimals), xloc.bar_time, yloc.price, color.rgb(0,217,252), label.style_label_left, color.black, size.small) : na
label.set_x(tp2, label.get_x(tp2) + math.round(ta.change(time) * lvlDistance))
label.set_y(tp2, tp2_y)
label.delete(tp2[1])

tp3s =  ta.crossover(close, tp2_y) and not bull and close>supertrend

plotshape(tp3s, text='', style=shape.xcross, location=location.abovebar, size=size.tiny, color=color.new(#ff000099, 68), textcolor=#2157f999)

tp3sd = ta.crossunder(close, tp2_y) and not bull and not bear and close<supertrend

plotshape(tp3sd, text='', style=shape.xcross, location=location.belowbar, size=size.tiny, color=color.new(#2157f999, 68), textcolor=#ff000099)


style = linesStyle == "SOLID" ? line.style_solid : linesStyle == "DASHED" ? line.style_dashed : line.style_dotted
lineEntry = levelsl or levels1 or levels2 or levels3 and lvlLines ? line.new(bar_index - (trigger == 0 ? countBull : countBear), lastTrade(close), bar_index + lvlDistance, lastTrade(close), xloc.bar_index, extend.none, color.new(color.gray, 0), style, 2) : na, line.delete(lineEntry[1])
lineStop  = levelsl and lvlLines ? line.new(bar_index - (trigger == 0 ? countBull : countBear), stop_y, bar_index + lvlDistance, stop_y, xloc.bar_index, extend.none, color.new(#E93E63, 0), style, 2) : na, line.delete(lineStop[1])
lineTp1   = levels1 and lvlLines ? line.new(bar_index - (trigger == 0 ? countBull : countBear), tp1_y, bar_index + lvlDistance, tp1_y, xloc.bar_index, extend.none, color.rgb(0,217,252), style, 2) : na, line.delete(lineTp1[1])
lineTp15  = levels2 and lvlLines ? line.new(bar_index - (trigger == 0 ? countBull : countBear), tp15_y, bar_index + lvlDistance, tp15_y, xloc.bar_index, extend.none, color.rgb(0,217,252), style, 2) : na, line.delete(lineTp15[1])
lineTp2   = levels3 and lvlLines ? line.new(bar_index - (trigger == 0 ? countBull : countBear), tp2_y, bar_index + lvlDistance, tp2_y, xloc.bar_index, extend.none, color.rgb(0,217,252), style, 2) : na, line.delete(lineTp2[1])

// ----------------------------------------- bands ----------------------------------- \\
useEma = true
int volBandsSen = 5
gray   = volBands ? #787B86 : na,   gray40   = volBands ? color.new(gray, 60) : na,      gray5     = volBands ? color.new(gray, 95) : na,        gray20  = volBands ? color.new(gray, 80) : na
//
f_kc(src, len, mult) =>
    float basis = useEma ? ta.ema(src, len) : ta.sma(src, len)
    float span  = useEma ? ta.ema(ta.tr, len) : ta.sma(ta.tr, len)
    [basis + span * mult, basis - span * mult]
    
[upperKC1, lowerKC1] = f_kc(close, 35, 4.5 )
[upperKC2, lowerKC2] = f_kc(close, 35, 6.4 )
[upperKC3, lowerKC3] = f_kc(close, 35, 8.3 )

b1 = plot(upperKC1, "", color.new(#E93E63, 75), editable=false)
b2 = plot(upperKC2, "", color.new(#E93E63, 85), editable=false)
b3 = plot(upperKC3, "", color.new(#E93E63, 90), editable=false)
b4 = plot(lowerKC1, "", color.new(#00DBFF, 75), editable=false)
b5 = plot(lowerKC2, "", color.new(#00DBFF, 85), editable=false)
b6 = plot(lowerKC3, "", color.new(#00DBFF, 90), editable=false)

// ---------------------------- moving average line ------------------------------------- \\
short = ta.ema(close, 23)
intm  = ta.ema(close, 200)
long  = ta.ema(close, mals)

// set bull
mal_is_bull = (short > intm ? color.new(#00DBFF, 75) : color.new(#E93E63, 75))

// plot it 
plot(mal ? long : na, color = mal_is_bull, linewidth=4)

//--------------------------- reversal signals --------------------------- \\

s1_vis = true
i1_s1 = 'Conversion Line'
f_s1 = 'crossover'
i2_s1 = 'Base Line'

s2_vis = true
i1_s2 = 'Lead 1'
f_s2 = 'crossover'
i2_s2 = 'Lead 2'

s3_vis = true
s4_vis = true

conversionPeriods = 5
basePeriods = 2
laggingSpan2Periods = 5
displacement = 6
donchian(len) =>
    math.avg(ta.lowest(len), ta.highest(len))
conversionLine = donchian(conversionPeriods)
baseLine = donchian(basePeriods)
leadLine1 = math.avg(conversionLine, baseLine)
leadLine2 = donchian(laggingSpan2Periods)

lead1 = leadLine1[displacement - 1]
lead2 = leadLine2[displacement - 1]

bool breakup = na
bool breakdn = na
crossup = ta.crossover(close, lead2)
green_candle = close > open
if lead2 > lead1 and green_candle
    breakup := crossup
    breakup
plotshape(enrev and breakup and s3_vis ? high : na, 'Signal3 (Break Up)', shape.diamond, location.belowbar, color.new(#00DBFF, 68), size=size.tiny)
bgcolor(enrev and breakup and s3_vis ? color.new(#00DBFF, 95) : na, title='Break Up Background', transp=90)

crossdn = ta.crossunder(close, lead2)
red_candle = close < open
if lead2 < lead1 and red_candle
    breakdn := crossdn
    breakdn
plotshape(enrev and breakdn and s4_vis ? low : na, 'Signal4 (Break Down)', shape.diamond, location.abovebar, color.new(#e93E63, 68), size=size.tiny)

// ------------------------------ support and resistance -------------------------- \\


left = 50
right = 25
quick_right = 5  // Used to try and detect a more recent significant swing.

pivot_high = ta.pivothigh(high, left, right)
pivot_lows = ta.pivotlow(low, left, right)

quick_pivot_high = ta.pivothigh(high, left, quick_right)
quick_pivot_lows = ta.pivotlow(low, left, quick_right)

level1 = ta.valuewhen(quick_pivot_high, high[quick_right], 0)
level2 = ta.valuewhen(quick_pivot_lows, low[quick_right], 0)
level3 = ta.valuewhen(pivot_high, high[right], 0)
level4 = ta.valuewhen(pivot_lows, low[right], 0)
level5 = ta.valuewhen(pivot_high, high[right], 1)
level6 = ta.valuewhen(pivot_lows, low[right], 1)
level7 = ta.valuewhen(pivot_high, high[right], 2)
level8 = ta.valuewhen(pivot_lows, low[right], 2)

level1_col = close >= level1 ? #0DBBFF : #E93E63
level2_col = close >= level2 ? #0DBBFF : #E93E63
level3_col = close >= level3 ? #0DBBFF : #E93E63
level4_col = close >= level4 ? #0DBBFF : #E93E63
level5_col = close >= level5 ? #0DBBFF : #E93E63
level6_col = close >= level6 ? #0DBBFF : #E93E63
level7_col = close >= level7 ? #0DBBFF : #E93E63
level8_col = close >= level8 ? #0DBBFF : #E93E63

plot(ensr ? level1 :na, style=plot.style_line, color=level1_col, show_last=1, linewidth=2, trackprice=true)
plot(ensr ?level2:na, style=plot.style_line, color=level2_col, show_last=1, linewidth=2, trackprice=true)
plot(ensr?level3:na, style=plot.style_line, color=level3_col, show_last=1, linewidth=2, trackprice=true)
plot(ensr?level4:na, style=plot.style_line, color=level4_col, show_last=1, linewidth=2, trackprice=true)
plot(ensr?level5:na, style=plot.style_line, color=level5_col, show_last=1, linewidth=2, trackprice=true)
plot(ensr?level6:na, style=plot.style_line, color=level6_col, show_last=1, linewidth=2, trackprice=true)
plot(ensr?level7:na, style=plot.style_line, color=level7_col, show_last=1, linewidth=2, trackprice=true)
plot(ensr?level8:na, style=plot.style_line, color=level8_col, show_last=1, linewidth=2, trackprice=true)

ig1 = ensra?line.new(bar_index, level1, bar_index + 1, level1, xloc.bar_index, extend.both, color.new(level1_col, 85), line.style_solid, 13):na
ig2 = ensra?line.new(bar_index, level2, bar_index + 1, level2, xloc.bar_index, extend.both, color.new(level2_col, 85), line.style_solid, 13):na
ig3 = ensra?line.new(bar_index, level3, bar_index + 1, level3, xloc.bar_index, extend.both, color.new(level3_col, 85), line.style_solid, 13):na
ig4 = ensra?line.new(bar_index, level4, bar_index + 1, level4, xloc.bar_index, extend.both, color.new(level4_col, 85), line.style_solid, 13):na
ig5 = ensra?line.new(bar_index, level5, bar_index + 1, level5, xloc.bar_index, extend.both, color.new(level5_col, 85), line.style_solid, 13):na
ig6 = ensra?line.new(bar_index, level6, bar_index + 1, level6, xloc.bar_index, extend.both, color.new(level6_col, 85), line.style_solid, 13):na
ig7 = ensra?line.new(bar_index, level7, bar_index + 1, level7, xloc.bar_index, extend.both, color.new(level7_col, 85), line.style_solid, 13):na
ig8 = ensra?line.new(bar_index, level8, bar_index + 1, level8, xloc.bar_index, extend.both, color.new(level8_col, 85), line.style_solid, 13):na